package com.xdhl.fu.gray.controller;

import com.alibaba.fastjson.JSONObject;
import com.xdhl.fu.common.core.constant.SecurityConstants;
import com.xdhl.fu.common.core.constant.event.MemberServiceEnvUpdateConstants;
import com.xdhl.fu.common.core.web.controller.BaseController;
import com.xdhl.fu.common.core.web.domain.AjaxResult;
import com.xdhl.fu.common.core.web.page.PageDomain;
import com.xdhl.fu.common.core.web.page.TableDataInfo;
import com.xdhl.fu.common.core.web.page.TableSupport;
import com.xdhl.fu.common.log.annotation.Log;
import com.xdhl.fu.common.log.enums.BusinessType;
import com.xdhl.fu.common.security.annotation.RequiresPermissions;
import com.xdhl.fu.gray.domain.vo.GrayServiceVO;
import com.xdhl.fu.gray.rocketmq.MsgProducer;
import com.xdhl.fu.member.api.RemoteListShuntService;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.remoting.common.RemotingHelper;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.stream.Collectors;

/**
 * 分流服务控制器
 *
 * @author 朱力维
 * @time 2022/6/7 12:07
 */
@RestController
@RequestMapping("/shunt")
public class ShuntController extends BaseController {

    @Resource
    private RemoteListShuntService remoteListShuntService;

    @Resource
    private MsgProducer msgProducer;

    /**
     * 分流用户
     */
    @Transactional
    @RequiresPermissions("gray:service:edit")
    @Log(title = "分流用户", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult shunt(@RequestBody GrayServiceVO grayServiceVO) throws Exception {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("envCode", grayServiceVO.getEnvCode());
        jsonObject.put("memberDataIds", CollectionUtils.isNotEmpty(grayServiceVO.getShuntMemberIds()) ?
                grayServiceVO.getShuntMemberIds().stream().map(String::valueOf).collect(Collectors.joining(",")) : null);
        jsonObject.put("updateModel", grayServiceVO.getUpdateModel());

        Message message = new Message();
        message.setTopic(MemberServiceEnvUpdateConstants.TOPIC);
        message.setTags("");
        message.setBody(jsonObject.toJSONString().getBytes(RemotingHelper.DEFAULT_CHARSET));
        msgProducer.getMqProducer().send(message);
        return AjaxResult.success("操作成功");
    }

    /**
     * 根据指定月内用户活跃度查询用户列表
     *
     * @param month 月份
     * @return 用户分页数据
     */
    @RequiresPermissions("gray:service:list")
    @GetMapping("/active/user-list")
    public TableDataInfo listActive(Integer month) {
        PageDomain pageDomain = TableSupport.buildPageRequest();
        Integer pageNum = pageDomain.getPageNum();
        Integer pageSize = pageDomain.getPageSize();

        TableDataInfo list = remoteListShuntService.listActive(month, pageNum, pageSize, SecurityConstants.INNER);
        return list;
    }

    /**
     * 根据职业查询用户列表
     *
     * @param occupation 职业
     * @return 用户分页数据
     */
    @RequiresPermissions("gray:service:list")
    @GetMapping("/occupation/user-list")
    public TableDataInfo listByOccupation(String occupation) {
        PageDomain pageDomain = TableSupport.buildPageRequest();
        Integer pageNum = pageDomain.getPageNum();
        Integer pageSize = pageDomain.getPageSize();

        TableDataInfo list = remoteListShuntService.listByOccupation(occupation, pageNum, pageSize, SecurityConstants.INNER);
        return list;
    }

    /**
     * 根据年龄区间查询用户列表
     *
     * @param min 左区间
     * @param max 右区间
     * @return 用户分页数据
     */
    @RequiresPermissions("gray:service:list")
    @GetMapping("/age-between/user-list")
    public TableDataInfo listByAge(Integer min, Integer max) {
        PageDomain pageDomain = TableSupport.buildPageRequest();
        Integer pageNum = pageDomain.getPageNum();
        Integer pageSize = pageDomain.getPageSize();

        TableDataInfo list = remoteListShuntService.listByAge(min, max, pageNum, pageSize, SecurityConstants.INNER);
        return list;
    }

}
